<template>
  <div>
    <el-form ref="citySRef" :inline="true" :model="cityCl.cityS">
      <el-form-item label="城市名称：" prop="name">
        <el-input v-model="cityCl.cityS.name" placeholder="城市名称" type="text"></el-input>
      </el-form-item>
      <el-form-item label="城市编码" prop="code">
        <el-input v-model="cityCl.cityS.code" placeholder="城市编码" type="text"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search()">搜索</el-button>
        <el-button type="primary" @click="resetS(citySRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div>
    <el-row :gutter="20">
      <el-col :span="6">
        <el-button type="primary" @click="opendialog()">
          <el-icon>
            <Plus />
          </el-icon>
          添加城市
        </el-button>
      </el-col>
    </el-row>
  </div>
  <div>
    <el-table :data="cityCl.citytable" stripe>
      <el-table-column align="center" label="序号" type="index" width="80">
        <template #default="scope">
          {{ (cityCl.cityS.page - 1) * cityCl.cityS.size + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="城市名称" prop="name" width="320" />
      <el-table-column align="center" header-align="center" label="城市编码" prop="code" width="320" />
      <el-table-column align="center" header-align="center" label="创建时间" prop="createTime" width="320" />
      <el-table-column align="center" fixed="right" header-align="center" label="操作" width="320">
        <template #default="scope">
          <el-button link size="small" type="primary" @click="edit(scope.row.id)">编辑 </el-button>
          <el-popconfirm title="确定要删除吗?" @confirm="deleteCitys(scope.row.id)">
            <template #reference>
              <el-button link type="primary">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div>
    <el-dialog v-model="cityCl.cityDialog" destroy-on-close title="添加城市" @close="resetDialog(addRef)">
      <el-form ref="addRef" :model="cityCl.editCity" :rules="cityCl.rules">
        <el-form-item label="父级" prop="pid">
          <el-cascader v-model="cityCl.editCity.pid" :getCheckedNodes="true" :options="cityCl.relationTrees" :props="cityCl.props" />
        </el-form-item>
        <el-form-item label="城市名称" prop="name">
          <el-input v-model="cityCl.editCity.name" type="text"></el-input>
        </el-form-item>
        <el-form-item label="城市代码" prop="code">
          <el-input v-model="cityCl.editCity.code" type="text"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveDepts()">保存 </el-button>
          <el-button @click="cityCl.cityDialog = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
  <Page :pageData="cityCl.cityS" @selectPage="search"></Page>
</template>
<script lang="ts" setup>
import { cityClass, cityInfo, relationTreeInfo } from '@/page/types/system/city'
import Page from '@/components/Page.vue'
import { deleteCity, detailCity, pageCity, relationCity, saveCity, updateCity } from '@/api/system'
import { ElMessage, FormInstance } from 'element-plus/es'

const cityCl = reactive(new cityClass())
const citySRef = ref()
const search = () => {
  cityCl.citytable = [] as cityInfo[]
  pageCity(cityCl.cityS).then(res => {
    cityCl.citytable = res.data.records as cityInfo[]
    cityCl.cityS.page = res.data.current
    cityCl.cityS.size = res.data.size
    cityCl.cityS.total = res.data.total
  })
}
const resetS = (rest: FormInstance | undefined) => {
  if (!rest) return
  rest.resetFields()
  search()
}
const opendialog = () => {
  const data = {} as relationTreeInfo
  relationCity(data).then(res => {
    cityCl.cityDialog = true
    cityCl.relationTrees = res.data as relationTreeInfo[]
  })
  cityCl.editCity = {} as cityInfo
}
const edit = (id: string) => {
  cityCl.editCity = {} as cityInfo
  detailCity(id).then(res => {
    cityCl.editCity = res.data as cityInfo
  })
  const data = {} as relationTreeInfo
  relationCity(data).then(res => {
    cityCl.cityDialog = true
    cityCl.relationTrees = res.data as relationTreeInfo[]
  })
}
const deleteCitys = (id: string) => {
  deleteCity(id).then(res => {
    search()
    ElMessage({
      message: '删除城市',
      type: 'success'
    })
  })
}
const resetDialog = (rest: FormInstance | undefined) => {
  if (!rest) return
  rest.resetFields()
  cityCl.editCity = {} as cityInfo
}
const saveDepts = () => {
  if (cityCl.editCity.id) {
    updateCity(cityCl.editCity).then(res => {
      search()
      ElMessage({
        message: '更新城市成功',
        type: 'success'
      })
    })
  } else {
    saveCity(cityCl.editCity).then(res => {
      search()
      ElMessage({
        message: '保存城市成功',
        type: 'success'
      })
    })
  }
  cityCl.cityDialog = false
}
onMounted(() => {
  search()
})
</script>

<style lang="scss" scoped>
.pagination-block {
  position: relative;

  .el-pagination {
    margin-top: 20px;
    position: absolute;
    right: 0%;
  }
}
.el-select {
  width: 200px;
}
</style>
